package chartCraft.tableDesk;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.colorchooser.ColorSelectionModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import chartCraft.ChartCraftController;
import chartCraft.model.TopModel;


public class EditColumnHeadDialog extends JDialog {
	private ChartCraftController mChartCraftController;
	private TopModel mModel;
	
	private int mSeriesId;
	
	private JTextField mHeaderString;
	private JButton mCancelBtn;
	private JButton mSetBtn;
	private JColorChooser mJColorChooser;
	
	public EditColumnHeadDialog(ChartCraftController chartCraftController, TopModel model) {
		mChartCraftController = chartCraftController;
		mModel = model;
		
		makeContents();
		registerBUttons();
		pack();
	}
	
	public void show(int seriesIndex) {
		mSeriesId = seriesIndex;
		
		//get and set current color
		Color color = mModel.getColorForSeries(mSeriesId);
		mJColorChooser.setColor(color);
		
		//get and set current name
		String name = mModel.getNameForSeries(seriesIndex);
		mHeaderString.setText(name);
		setVisible(true);
	}

	private void makeContents() {
		JPanel contentPanel = new JPanel();
        contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.PAGE_AXIS));
        
        //header
        JPanel headerPanel = new JPanel();
        headerPanel.add(new JLabel("Column Header: "));
        
        mHeaderString = new JTextField(15);
        headerPanel.add(mHeaderString);
        headerPanel.add(Box.createHorizontalGlue());
        
        contentPanel.add(headerPanel);
        
        //color
        mJColorChooser = new JColorChooser();
        PreviewPanel previewPanel = new PreviewPanel(mJColorChooser);
        previewPanel.setSize(previewPanel.getPreferredSize());
        previewPanel.setBorder(BorderFactory.createEmptyBorder(0,0,1,1));
        mJColorChooser.setPreviewPanel(previewPanel);
        contentPanel.add(mJColorChooser);
        
        //header and color
        contentPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
        add(contentPanel, BorderLayout.CENTER);
        
        //buttons
        JPanel buttonPane = new JPanel();
        buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS));
        buttonPane.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));
        buttonPane.add(Box.createHorizontalGlue());
        mCancelBtn = new JButton("Cancel");
        buttonPane.add(mCancelBtn);
        buttonPane.add(Box.createRigidArea(new Dimension(10, 0)));
        mSetBtn = new JButton("Set");
        buttonPane.add(mSetBtn);
        
        add(buttonPane, BorderLayout.SOUTH);

	}

	private void registerBUttons() {
		mCancelBtn.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				setVisible(false);				
			}
		});
		
		mSetBtn.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				mChartCraftController.changeSeriesMetaInformation(mSeriesId, mJColorChooser.getColor(), mHeaderString.getText());
				setVisible(false);				
			}
		});
	}

}

class PreviewPanel extends JComponent {
	/**Adapted from 
	 * http://www.exampledepot.com/egs/javax.swing.colorchooser/CustPreview.html
	 */
	private static final long serialVersionUID = 5762445386556006059L;

    // The currently selected color
    private Color curColor;

    public PreviewPanel(JColorChooser chooser) {
        // Initialize the currently selected color
        curColor = chooser.getColor();

        // Add listener on model to detect changes to selected color
        ColorSelectionModel model = chooser.getSelectionModel();
        model.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent evt) {
                ColorSelectionModel model = (ColorSelectionModel)evt.getSource();

                // Get the new color value
                curColor = model.getSelectedColor();
            }
        }) ;

        // Set a preferred size
        setPreferredSize(new Dimension(50, 50));
    }

    // Paint current color
    public void paint(Graphics g) {
        g.setColor(curColor);
        g.fillRect(0, 0, getWidth()-1, getHeight()-1);
    }
}


